<script>
	import {getAppVersion} from '@/api/my_setting.js';
	import {UploadGps} from '@/util/UploadGps.js';
	import {WS} from '@/util/WS.js';
	import Push from '@/util/push.js';
	import URL from '@/util/url.js';
	import { mapMutations } from 'vuex';
    export default {
		data() {
			return {
				uploadGps: null
			}
		},
        onLaunch: function() {
			//定时检测更新
			setInterval(() => {
				//获取是安卓还是ios
				// let system= uni.getSystemInfoSync().platform;
				// if(system == 'ios'){
				// 	this.type = "businessDisposal_ios";
				// }
				// if(system == 'android'){
				// 	this.type = "businessDisposal";
				// }
				// //版本信息
				// this.getAppVersion();
				// this.undateVersion();
			}, 30000);

			if(plus.runtime.appid !== 'HBuilder'){ // 真机运行不需要检查更新，真机运行时appid固定为'HBuilder'，这是调试基座的appid
				uni.request({
				    url: 'https://7a3e3fa9-7820-41d0-be80-11927ac2026c.bspapp.com/http/update', //检查更新的服务器地址
				    data: {
				        appid: plus.runtime.appid,
				        version: plus.runtime.version,
				        imei: plus.device.imei
				    },
				    success: (res) => {
				        if (res.statusCode == 200 && res.data.isUpdate) {
				            // 提醒用户更新
				            uni.showModal({
				                title: '更新提示',
				                content: res.data.note ? res.data.note : '是否选择更新',
				                success: (ee) => {
				                    if (ee.confirm) {
				                        plus.runtime.openURL(res.data.url);
				                    }
				                }
				            })
				        }
				    }
				})
			}

			// 一键登录预登陆，可以显著提高登录速度
			uni.preLogin({
				provider: 'univerify',
				success: (res) => {
					this.setUniverifyErrorMsg();
				},
				fail: (res) => {
					this.setUniverifyLogin(false);
					this.setUniverifyErrorMsg(res.errMsg);
				},
			})

			let push = new Push({fn: (pushId)=> {
				this.$store.dispatch({type: 'push/setPushId', pushId});
			}});

			push.cycle();

			plus.push.addEventListener('click', function(msg) {},false);

			plus.push.addEventListener('receive', function(msg) {
				if(msg.payload && msg.payload.message_type == 20 && msg.payload.message_url == 'video_call'){
					uni.showModal({
						title: '提示',
						content: `${msg.payload.message_content}`,
						success: function (res) {
							let pages = getCurrentPages();
							let page = pages[pages.length - 1];

							if(page.route == 'pages/alert/call'){
								uni.$emit('alert.call.joinChannel');
							} else {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/alert/call'
									})
								} else if (res.cancel) {

								}
							}
						}
					});
				}

			},false);
			

			this.uploadGps = new UploadGps();

			uni.$on('uploadGps.check', ()=> {
				this.uploadGps.getClockInInfo().then(()=> {
					this.uploadGps.cycle();
				}, ()=> {
					this.uploadGps.pause();
				})
			});

			let ws = new WS({
				wsUrl: URL.wsUrl
			});

			ws.connect();
			ws.onMessage();
        },
        onShow: function() {
			this.uploadGps.getClockInInfo().then(()=> {
				this.uploadGps.cycle();
			}, ()=> {
				this.uploadGps.pause();
			})
		},
        onHide: function() {},
		globalData: {
			test: ''
		},
		methods:{
			...mapMutations(['setUniverifyErrorMsg','setUniverifyLogin']),
			//获取版本信息
			getAppVersion(){
				plus.runtime.getProperty( plus.runtime.appid, ( wgtinfo ) => {
					this.app_version = wgtinfo.version;
				} );
			},
			//检查更新
			undateVersion(){
				let appVersion = ''

				uni.getSystemInfo({
					success: function(e) {
						appVersion = e.platform
					}
				})
				let _this = this;

				getAppVersion({type: this.type}).then( res => {
					if (res.data.app_version != this.app_version) {
						uni.showModal({
							title: "版本更新",
							content: res.data.update_content, //更新描述
							confirmText:'立即更新',
							cancelText:'稍后进行',
							success: sucRes => {
								if (sucRes.confirm) {
									//如果是安卓直接更新下载
									if(appVersion === 'android'){
										_this.doUpData();
									}else{
										let appleId= 123456789
										plus.runtime.launchApplication({
											action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
										}, function(e) {
											console.log('Open system default browser failed: ' + e.message);
										});
									}
								}
							}
						})
					}else{
						if(clickType === 'click'){
							uni.showToast({icon:'none',title:'处置系统：当前已是最新版本'})
						}else{
							this.app_version= `版本 ${wgtinfo.version}`
						}
					}
				})

			},
			doUpData() {
			    uni.showLoading({
			        title: '更新中……'
			    })

			    uni.downloadFile({//执行下载
			        url: URL.Setting, //下载地址
			        success: data => {//下载成功
			            uni.hideLoading();
			            if (data.statusCode == 200) {
			                uni.showModal({
			                    title: '',
			                    content: '更新成功，确定现在安装吗？',
			                    confirmText: '安装',
			                    confirmColor: '#EE8F57',
			                    success: function(res) {
			                        if (data.statusCode === 200) {
			                            plus.runtime.install(data.tempFilePath, {
			                                force: true
			                            }, function(res) {
			                                utils.showToast('更新成功，重启中');
											plus.runtime.restart();
			                            });
			                        }
			                    }
			                });
			            }
			        }
			    });
			},

		}
    }
</script>

<style>
    /* #ifndef APP-PLUS-NVUE */
    /* uni.css - 通用组件、模板样式库，可以当作一套ui库应用 */
    @import './common/uni.css';

	/* H5 兼容 pc 所需 */
	/* #ifdef H5 */
	@media screen and (min-width: 768px) {
		body{
			overflow-y: scroll;
		}
	}

	 /* 顶栏通栏样式 */
	/* .uni-top-window {
	    left: 0;
	    right: 0;
	} */

	uni-page-body {
		background-color: #F5F5F5 !important;
		/* min-height: 100% !important;
		height: auto !important; */
	}

	.uni-top-window uni-tabbar .uni-tabbar {
		background-color: #fff !important;
	}

	.uni-app--showleftwindow .hideOnPc {
		display: none !important;
	}
	/* #endif */

    /* 以下样式用于 hello uni-app 演示所需 */
    page {
        background-color: #efeff4;
        height: 100%;
        font-size: 28rpx;
        line-height: 1.8;
    }
	.fix-pc-padding {
		padding: 0 50px;
	}
    .uni-header-logo {
        padding: 30rpx;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin-top: 10rpx;
    }

    .uni-header-image {
        width: 100px;
        height: 100px;
    }

    .uni-hello-text {
        color: #7A7E83;
    }

    .uni-hello-addfile {
        text-align: center;
        line-height: 300rpx;
        background: #FFF;
        padding: 50rpx;
        margin-top: 10px;
        font-size: 38rpx;
        color: #808080;
    }
    /* #endif*/
	.amap-logo,.amap-copyright {
		/* #ifndef */
		 display: none !important;
		 /* #endif */
	}
	.uni-page-head-ft{
		line-height: 44px;
	}
</style>
